package com.yanqu.road.server.manger.RecoveryReward;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.retrievereward.config.RecoveryRewardConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.retrievereward.config.RecoveryRewardParameterConfig;
import com.yanqu.road.logic.bussiness.activity.RecoveryRewardBusiness;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RecoveryRewardMgr extends TempMgr {

    private static RecoveryRewardConfig config;

    private static ActivityInfo activityInfo;

    @Override
    public boolean init() throws Exception {
        return true;
    }

    @Override
    public boolean reload() throws Exception {
        return super.reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean save() {
        return super.save();
    }

    public static void reloadActivityData() {
        getLogger().info("reload recoverReward activity start");
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.RecoveryReward.getValue());
        if (openActivityInfoList.size() <= 0) {
            getLogger().info("reload recallPlayer activity finish, no activity in show time.");
            config = null;
            activityInfo = null;
            return;
        }
        ActivityInfo tempActivityInfo = openActivityInfoList.get(0);
        boolean activityChange = false;
        if (activityInfo == null || activityInfo.getActivityId() != tempActivityInfo.getActivityId()) {
            activityChange = true;
        }

        activityInfo = tempActivityInfo;
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        //初始化config
        Map<String, ActivityConfig> activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());
        Map<Integer,RecoveryRewardParameterConfig> tmpParamMap = RecoveryRewardBusiness.getRecoveryRewardConfigMap(activityInfo.getActivityId());

        int tmpRecoveryLimit = 35;
        int tmpRecoveryMaxTime = 7;
        if (!activityConfigMap.containsKey("RECOVERY_OPEN_LIMIT")){
            getLogger().error("recoveryReward activity RECOVERY_OPEN_LIMIT config error activityId = {}",activityInfo.getActivityId());
        } else {
            tmpRecoveryLimit = activityConfigMap.get("RECOVERY_OPEN_LIMIT").getIntValue();
        }
        if (!activityConfigMap.containsKey("RECOVERY_TIME_MAX")){
            getLogger().error("recoveryReward activity RECOVERY_TIME_MAX config error activityId = {}",activityInfo.getActivityId());
        } else {
            tmpRecoveryMaxTime = activityConfigMap.get("RECOVERY_TIME_MAX").getIntValue();
        }
        RecoveryRewardConfig tmpConfig = new RecoveryRewardConfig(tmpParamMap,tmpRecoveryLimit,tmpRecoveryMaxTime);
        config = tmpConfig;
    }



    public static RecoveryRewardConfig getConfig() {
        return config;
    }

    public static RecoveryRewardParameterConfig getRecoveryRewardParameterConfig(int type){
        if (config==null){
            return null;
        }
        return config.getParameterConfigMap().get(type);
    }

    public static void setConfig(RecoveryRewardConfig config) {
        RecoveryRewardMgr.config = config;
    }

    public static ActivityInfo getActivityInfo() {
        return activityInfo;
    }
}
